/**
 * Copyright (c) 2007, Ashish Lijhara
 * All rights reserved.
 *
 *AetasSplash.java
 *
 */

package crazy.java.Aetas;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;
import javax.swing.UIManager;

/**
 *
 * @author Ashish (Java Crazy)
 */
public class AetasSplash extends JWindow{
    static BufferedImage splashImage=null;
    
    
    public AetasSplash(){
        try {
            File file = new File("res/splash.png");
            splashImage = ImageIO.read(new URL(file.toURI().toURL().toString()));
        } catch(IOException e) {
            e.printStackTrace();
        }
        UIManager.put("ProgressBarUI","crazy.java.Aetas.CoolBar");
        
        
        JPanel panel=new JPanel(new FlowLayout());
       
        //JPanel progpanel=new JPanel();
        //progpanel.add(pbar);
        
        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(panel,BorderLayout.SOUTH);
        //setOpaque(false);
    }
    public static Dimension getSplashSize(){
        return new Dimension(splashImage.getWidth(),splashImage.getHeight());
    }
    public static void updateBar(int newValue,JProgressBar pbar) {
        pbar.setValue(newValue);
    }
    
    
    public static void invokeMain(String className, String[] args) {
        try {
            Class.forName(className).getMethod("main", new Class[] {String[].class}).invoke(null, new Object[] {args});
        } catch (Exception e) {
            InternalError error = new InternalError("Failed to invoke main method");
            error.initCause(e);
            throw error;
        }
    }
    
    public void paint(Graphics g){
        super.paint(g);
        Graphics2D g2=(Graphics2D)g;
        g2.drawImage(splashImage,0,0,null);
    }
    /**
     * 
     * @param args 
     */
    public static void main(String[] args){
        final String[] s=args;
        //JFrame splashFrame=new JFrame();
        final AetasSplash splash=new AetasSplash();
        //splashFrame.setContentPane(panel);
        
        Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
        splash.setLocation(
                (screenDim.width - getSplashSize().width) / 2,
                (screenDim.height - getSplashSize().height) / 2
                );
        splash.setSize(getSplashSize());
        //splashFrame.setUndecorated(true);
        splash.setVisible(true);
        
        Thread t=new Thread(new Runnable() {
            public void run() {
                
                try {
                    
                        Thread.sleep(4000);
                    
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally{
                    invokeMain("crazy.java.Aetas.AetasMain", s);
                    splash.setVisible(false);
                }
                
            }
        });
        t.start();
        
        
    }
}
